/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cardsender_srv_stub.h"
#include <string.h>
#include "message_option.h"
#include "message_parcel.h"
#include "hilog_wrapper.h"
#include "cardsender_common.h"
#include "cardsender_errors.h"
using namespace OHOS;
using namespace std;
namespace SwanLink {
namespace Finance {
int CardsenderSrvStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    CARDSENDER_HILOGD(CARDSENDER_SRV, "cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
    std::u16string descriptor = CardsenderSrvStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    if (descriptor != remoteDescriptor) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Descriptor is not matched");
        return ERR_NAME_NOT_FOUND;
    }

    return CheckRequestCode(code, data, reply, option);
}

int32_t CardsenderSrvStub::CheckRequestCode(const uint32_t code, MessageParcel& data, MessageParcel& reply,
    MessageOption& option)
{
    switch (code) {
    case static_cast<int>(ICardsenderSrv::CARDSENDER_OPEN): {
        return OpenStub(data, reply, option);
    }
    case static_cast<int>(ICardsenderSrv::CARDSENDER_CLOSE): {
        return CloseStub(data, reply, option);
    }
    case static_cast<int>(ICardsenderSrv::CARDSENDER_RESET): {
        return ResetStub(data, reply, option);
    }
    case static_cast<int>(ICardsenderSrv::CARDSENDER_DISPENSECARD): {
        return DispenseCardStub(data, reply, option);
    }
    case static_cast<int>(ICardsenderSrv::CARDSENDER_GETSUPPORTACCESS): {
        return GetSupportAccessStub(data, reply, option);
    }
    case static_cast<int>(ICardsenderSrv::CARDSENDER_QUERYSTATUS): {
        return QueryStatusStub(data, reply, option);
    }
    }
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}


int32_t CardsenderSrvStub::OpenStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    std::string name;
    Transport* trans;

    READ_PARCEL_WITH_RET(data, String, name, -1);
    READ_BUFFER_WITH_RET(data, Transport, trans, sizeof(Transport));
    int result = Open(name, *trans);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, -1);

    return ERR_OK;
}

int32_t CardsenderSrvStub::CloseStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, -1);

    int result = Close(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, -1);

    return ERR_OK;
}

int32_t CardsenderSrvStub::ResetStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, -1);

    int result = Reset(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, -1);

    return ERR_OK;
}

int32_t CardsenderSrvStub::DispenseCardStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;

    READ_PARCEL_WITH_RET(data, Int32, handle, -1);

    int result = DispenseCard(handle);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, -1);

    return ERR_OK;
}

int32_t CardsenderSrvStub::GetSupportAccessStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    vector<DevAccess> result = GetSupportAccess();
    if (!WriteVectorToParcel(reply, result)) {
        return -1;
    }
    return ERR_OK;
}

int32_t CardsenderSrvStub::QueryStatusStub(MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
    int handle = -1;
    int suppBinStat = 0;
    int retainBinStat = 0;

    READ_PARCEL_WITH_RET(data, Int32, handle, -1);

    int result = QueryStatus(handle, &suppBinStat, &retainBinStat);

    WRITE_PARCEL_WITH_RET(reply, Int32, suppBinStat, -1);
    WRITE_PARCEL_WITH_RET(reply, Int32, retainBinStat, -1);
    CARDSENDER_HILOGI(CARDSENDER_SRV, "CardsenderSrvStub::QueryStatusStub result %{public}d", result);
    WRITE_PARCEL_WITH_RET(reply, Int32, result, -1);

    return ERR_OK;
}
}  // namespace Finance
}  // namespace SwanLink
